<?php
/**
 * Created by PhpStorm.
 * User: sunshubo
 * Date: 2017/9/19
 * Time: 22:42
 */

class Update extends MY_Controller
{
    function index($page)
    {
        $post_data = $this->input->post();
//        if ($_FILES) {
//            $result = ($this->upload_file($_FILES));
//            if ($result['code'] % 2 !== 0) {
//                echo json_encode(array('result' => ERROR, 'msg' => '文件上传失败！', 'code' => $result['code']));
//                return;
//            }
//            $post_data['file'] = $result['list'];
//        }
        if (empty($page)) {
            $this->load->model('task/TaskInfo_model', 'task');
            echo $this->task->add_task($post_data);
        } elseif ($page === 'zuowen') {
            $this->load->model('task/Task_zuowen_model', 'task');
            echo $this->task->add_task($post_data);
        } elseif ($page === 'aoshu') {
            $this->load->model('task/Task_aoshu_model', 'task');
            echo $this->task->add_task($post_data);
        } elseif ($page === 'shufa') {
            $this->load->model('task/Task_shufa_model', 'task');
            echo $this->task->add_task($post_data);
        }
        return;
    }

    function upload_file($file)
    {
        $status_code = 0;
        $name_list = null;
        foreach ($file["files"]["error"] as $key => $val) {
            $name_list .= $file["files"]["name"][$key] . ',';
            if ($file["files"]["error"][$key] === 0) {
                //没有错误
                if ((($file["files"]["type"][$key] == "image/gif")
                        || ($file["files"]["type"][$key] == "image/jpeg")
                        || ($file["files"]["type"][$key] == "image/png")
                        || ($file["files"]["type"][$key] == "image/pjpeg"))
                    && ($file["files"]["size"][$key] < 5242880)) {
                    if (file_exists("upload/" . $file["files"]["name"][$key])) {
                        //文件重名，文件已存在
//                        return 101;
//                            echo $file["files"]["name"][$key] . " already exists. ";
                    } else {
                        $upload_file
                            = iconv("UTF-8", "GB2312", $file["files"]["name"][$key]);
                        move_uploaded_file($file["files"]["tmp_name"][$key], "upload/" . $upload_file);
                        //文件上传成功
                        $status_code += 100;
                    }
                } else {
                    //文件不符合要求
                    return 101;
                }
            } else {
                //文件错误
                return 102;
            }
        }
        return array('code' => $status_code, 'list' => $name_list);
    }
}